using System;
using System.Collections.Generic;
//using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using LynxBlogAPI.Models;

namespace LynxBlogAPI.Controllers
{
    public class CurrentPostDataController : ApiController
    {
        private LynxWebLog_Context db = new LynxWebLog_Context();

        // GET api/CurrentPostData
        [Queryable]
        public IEnumerable<CurrentPostData> GetCurrentPostData()
        {
            return db.CurrentPostDatas.AsEnumerable();
        }

        // GET api/CurrentPostData/5
        [Queryable]
        public CurrentPostData GetCurrentPostData(Int32 id)
        {
            CurrentPostData currentpostdata = db.CurrentPostDatas.Find(id);
            if (currentpostdata == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return currentpostdata;
        }

        // PUT api/CurrentPostData/5
        public HttpResponseMessage PutCurrentPostData(Int32 id, CurrentPostData currentpostdata)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != currentpostdata.postid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(currentpostdata).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/CurrentPostData
        public HttpResponseMessage PostCurrentPostData(CurrentPostData currentpostdata)
        {
            if (ModelState.IsValid)
            {
                db.CurrentPostDatas.Add(currentpostdata);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, currentpostdata);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = currentpostdata.postid }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/CurrentPostData/5
        public HttpResponseMessage DeleteCurrentPostData(Int32 id)
        {
            CurrentPostData currentpostdata = db.CurrentPostDatas.Find(id);
            if (currentpostdata == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.CurrentPostDatas.Remove(currentpostdata);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, currentpostdata);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}